<template>
  <div v-loading="loading" :class="className" :style="{height: height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons')
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'dealTeam'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      loading: false,
      optionData: {
        dateList: []
      }
    }
  },
  watch: {
    chartData() {
      const data = {
        amount: [],
        count: [],
        deptName: []
      }
      this.chartData.forEach(item => {
        data.amount.push(item.investedTotalAmount / 1000000)
        data.count.push(item.dealNums)
        data.deptName.push(this.$t(item.deptName))
      })
      this.chart.setOption({
        xAxis: [
          {
            data: data.deptName
          }
        ],
        series: [{
          name: this.$t('金额(百万)'),
          data: data.amount
        },
          {
            name: this.$t('数量'),
            data: data.count
          }]
      })
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, null)

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: '#dddc6b'
            }
          }
        },
        grid: {
          left: '10',
          top: '70',
          right: '30',
          bottom: '10',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: ['5%', '5%'],
            axisTick: { show: false },
            axisLine: {
              lineStyle: {
                color: 'rgba(255,255,255,.1)'
              }
            },
            axisLabel: {
              textStyle: {
                color: '#fff',
                fontSize: 12
              }
            },

            data: ['团队1', '团队2', '团队3', '团队4', '团队5']
          }
        ],

        yAxis: [
          {
            name: this.$t('金额(百万)'),
            type: 'value',
            axisTick: { show: false },
            splitLine: {
              lineStyle: {
                color: 'rgba(255,255,255,0.2)'
              }
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              color: '#fff'
            },
            splitArea: {
              areaStyle: {
                color: 'rgba(255,255,255,0)'
              }
            }
          },
          {
            name: this.$t('数量(个)'),
            type: 'value',
            axisTick: { show: false },
            splitLine: {
              lineStyle: {
                show: false,
                color: 'rgba(255,255,255,0)'
              }
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              color: '#fff'
            },
            splitArea: {
              areaStyle: {
                color: 'rgba(255,255,255,0)'
              }
            }

          }
        ],
        series: [
          {
            name: this.$t('金额(万)'),
            type: 'bar',
            barWidth: '20px',
            itemStyle: {
              normal: {
                color: function(params) {
                  var colorList = ['#5470C6', '#91CC75', '#FAC858', '#EE6666', '#73C0DE', '#3BA272']
                  return colorList[params.dataIndex]
                }
              }
            }
          },
          {
            name: this.$t('数量'),
            type: 'line',
            smooth: true,
            yAxisIndex: 1,
            symbol: 'circle',
            symbolSize: 3,
            showSymbol: false,
            data: []
          }
        ],
        animationEasing: 'cubicInOut',
        animationDuration: 3000
      })
    }
  }
}
</script>
